﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WFCSharp_I
{
    public partial class ADOForm : Form
    {
        private DataTable categoryTable;
        private Student[] students = new Student[3];
                                         
        public ADOForm()
        {
            InitializeComponent();
        }

        private void ADOForm_Load(object sender, EventArgs e)
        {
            bindingSource.Add(new Student("MR A", 1));
            bindingSource.Add(new Student("MR B", 2));
            bindingSource.Add(new Student("MR C", 3));

            categoryCombo.DataSource = bindingSource;
            categoryCombo.DisplayMember = "Name";

            dataGridView1.DataSource = bindingSource;
            //GetCategoriesByReader();
            //GetCategoriesByAdapter();
        }

        private void GetCategoriesByAdapter()
        {
            using (SqlConnection connection = new SqlConnection(
                "Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;"))
            {
                //connection.ConnectionString = "Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;";

                connection.Open();
                SqlCommand selectCommand = new SqlCommand();
                selectCommand.CommandText = "SELECT CategoryID, CategoryName, [Description] FROM Categories";
                selectCommand.Connection = connection;

                //selectCommand.ExecuteNonQuery(); // -> UPDATE, DELETE, INSERT
                //selectCommand.ExecuteScalar(); // -> Lấy ô ở dòng 1, cột 1 của table trả về
                //selectCommand.ExecuteXmlReader(); // -> Lấy dữ liệu dạng Xml

                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
                categoryTable = new DataTable();
                sqlDataAdapter.Fill(categoryTable);

                categoryCombo.DataSource = categoryTable;
                categoryCombo.DisplayMember = "CategoryName";

                dataGridView1.DataSource = categoryTable;

                //DataView categoryView = new DataView(categoryTable);
                //categoryView.Sort = "CategoryName DESC";    // sắp xếp giảm dần

                //foreach (DataRowView row in categoryView)
                //    categoryCombo.Items.Add(row["CategoryName"].ToString());
            }
        }

        private void GetCategoriesByReader()
        {
            using (SqlConnection connection = new SqlConnection(
                "Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;"))
            {
                //connection.ConnectionString = "Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;";

                connection.Open();
                SqlCommand selectCommand = new SqlCommand();
                selectCommand.CommandText = "SELECT CategoryID, CategoryName, [Description] FROM Categories";
                selectCommand.Connection = connection;

                //selectCommand.ExecuteNonQuery(); // -> UPDATE, DELETE, INSERT
                //selectCommand.ExecuteScalar(); // -> Lấy ô ở dòng 1, cột 1 của table trả về
                //selectCommand.ExecuteXmlReader(); // -> Lấy dữ liệu dạng Xml

                SqlDataReader reader = selectCommand.ExecuteReader(); // -> Lấy dữ liệu trả về dạng bảng

                while (reader.Read())
                {
                    // Đọc dữ liệu của một dòng
                    int categoryId = reader.GetInt32(0);

                    //string categoryName = reader.GetString(1);
                    string categoryName;
                    if (!reader.IsDBNull(1))
                        categoryName = (string)reader[1];
                    else
                        categoryName = null;

                    string description = reader.GetString(2);

                    // Muốn mỗi dòng trong db tương ứng với 1 item trong combobox
                    categoryCombo.Items.Add(categoryName);
                }
            }
        }
        
        private void UpdateCategoryName(int categoryId, string categoryName)
        {
            using (SqlConnection connection = new SqlConnection(
                "Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;"))
            {
                //connection.ConnectionString = "Server=DELL-N5110;Database=Northwind;Trusted_Connection=True;";

                connection.Open();
                SqlCommand selectCommand = new SqlCommand();
                selectCommand.CommandText = "UPDATE Categories SET CategoryName = @CategoryName WHERE CategoryID = @CategoryId";
                selectCommand.Connection = connection;

                // Khai báo tham số, cách 1
                selectCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value = categoryName;

                // Khai bao tham số, cách 2
                SqlParameter parameter = new SqlParameter("@CategoryId", SqlDbType.Int);
                parameter.Value = categoryId;
                selectCommand.Parameters.Add(parameter);

                selectCommand.ExecuteNonQuery(); // -> UPDATE, DELETE, INSERT
                //selectCommand.ExecuteScalar(); // -> Lấy ô ở dòng 1, cột 1 của table trả về
                //selectCommand.ExecuteXmlReader(); // -> Lấy dữ liệu dạng Xml
            }
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            //UpdateCategoryName(int.Parse(idText.Text), nameText.Text);
            //students[0].Name += DateTime.Now.Second;
            //categoryTable.Rows[0][1] = categoryTable.Rows[0][1].ToString() + DateTime.Now.Second;

            ((Student) bindingSource[0]).Name += DateTime.Now.Second;
        }
    }
}
